package cn.tedu.toa.management.sys.service.impl;

import cn.tedu.toa.management.sys.entity.FKeyservices;
import cn.tedu.toa.management.sys.entity.FNews;
import cn.tedu.toa.management.sys.entity.SysUsers;
import cn.tedu.toa.management.sys.mapper.FNewsMapper;
import cn.tedu.toa.management.sys.mapper.SysUsersMapper;
import cn.tedu.toa.management.sys.service.FNewsService;
import cn.tedu.toa.management.sys.service.ex.DeleteException;
import cn.tedu.toa.management.sys.service.ex.ParamIllegelException;
import cn.tedu.toa.management.sys.service.ex.UpdateException;
import cn.tedu.toa.management.sys.vo.KeyServiceVo;
import cn.tedu.toa.management.sys.vo.NewsVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 * 新闻 服务实现类
 * </p>
 *
 * @author huaqian
 * @since 2021-10-02
 */
@Service
public class FNewsServiceImpl extends ServiceImpl<FNewsMapper, FNews> implements FNewsService {

    @Autowired
    private FNewsMapper newsMapper;

    @Autowired
    private SysUsersMapper usersMapper;

    private List<FNews> newsList = new CopyOnWriteArrayList<>();
    private Map<String,FNews> newsMap = new ConcurrentHashMap<>();
    @Override
    public List<FNews> getNews() {
        if (newsList.isEmpty()){
            synchronized (newsList){
                if(newsList.isEmpty()){
                    List<FNews> list = newsMapper.selectList(null);
                    newsList.addAll(list);
                    for (FNews news:list
                        ) {
                        newsMap.put(news.getTitle(),news);
                    }
                }
            }
        }
        return newsList;
    }

    @Override
    public NewsVo news(String title) {
        if(newsMap.isEmpty()){
            getNews();
        }
        FNews news = newsMap.get(title);
        NewsVo newsVo = new NewsVo()
                .setTitle(news.getTitle())
                .setContent(news.getContent())
                .setCreatetime(news.getCreatetime());
        int index = newsList.indexOf(news);
        if(index==0){
            String prevName = newsList.get(newsList.size()-1).getTitle();
            String nextName = newsList.get(index+1).getTitle();
            newsVo.setPrevName(prevName).setNextName(nextName);
        }else if(index==newsList.size()-1){
            String prevName = newsList.get(index-1).getTitle();
            String nextName = newsList.get(0).getTitle();
            newsVo.setPrevName(prevName).setNextName(nextName);
        }else {
            String prevName = newsList.get(index-1).getTitle();
            String nextName = newsList.get(index+1).getTitle();
            newsVo.setPrevName(prevName).setNextName(nextName);
        }
        return newsVo;
    }

    @Override
    public PageInfo<FNews> findNewsList(String name, Integer pageNum, Integer pageSize) {
        //1 参数验证
        if(pageNum==null || pageSize==null || pageSize<1 ){
            throw new ParamIllegelException("新闻列表查询异常!查询的分页参数不合法!");
        }
        //pageHelp分页
        PageHelper.startPage(pageNum,pageSize);
        //调用持久层
        List<FNews> list=newsMapper.selectNewsList(name);
        //查询结果存入PageInfo
        return new PageInfo<>(list);
    }

    @Override
    public void edit(FNews news,String username) {
        news.setModifyusername(username);
        news.setModifytime(LocalDateTime.now());
        int num = newsMapper.update(
                news.getType(), news.getTitle(),
                news.getSummary(), news.getContent(),
                news.getModifyusername(),news.getModifytime(), news.getId()
        );
        if(num!=1){
            throw new UpdateException("修改失败");
        }
        newsList = new CopyOnWriteArrayList<>();
        newsMap = new ConcurrentHashMap<>();
    }

    @Override
    public void insert(FNews news, String userName) {
        SysUsers user = usersMapper.selectUserByName(userName);
        news.setUserId(user.getId())
                .setCreatetime(LocalDateTime.now());
        newsMapper.insert(news);
    }

    //删除
    @Override
    public void delete(Integer id) {
        int num = newsMapper.deleteById(id);
        if(num!=1){
            throw new DeleteException("删除新闻异常");
        }
    }

    @Override
    public FNews getNewsByTitle(String title) {
        if(newsMap.isEmpty()){
            getNews();
        }
        return newsMap.get(title);
    }
}
